package com.wnnight.wys.mapper.payment;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnnight.wys.entity.payment.PayMentRecords;
import com.wnnight.wys.vo.PayMentVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName: PayMentMapper
 * @author: LJC
 * @date: 2022/8/29-10:35
 */
public interface PayMentMapper extends BaseMapper<PayMentRecords> {
    //根据业主id查询
    @Select("SELECT * FROM wys_payment_records wpr, wys_landlord wld, wys_charge_config wcc, wys_payment_method wpm, wys_room wr WHERE wpr.landlord_id = wld.id AND wpr.charge_conf_id = wcc.id AND wpr.room_id = wr.id AND wpr.payment_method_id = wpm.id AND wpr.landlord_id = #{landlord.id}")
    @ResultMap("payMentResultMap")
    List<PayMentRecords> getByLandlordId(PayMentRecords payMentRecords);

    //多表联查
    @Select("SELECT * FROM wys_payment_records wpr, wys_landlord wld, wys_charge_config wcc, wys_payment_method wpm, wys_room wr WHERE wpr.landlord_id = wld.id AND wpr.charge_conf_id = wcc.id AND wpr.room_id = wr.id AND wpr.payment_method_id = wpm.id ORDER BY collection_time ASC")
    @Results(id="payMentResultMap",value={
            @Result(column = "id", property = "id", id = true),
            @Result(column = "bill_number", property = "billNumber"), //账单编号
            @Result(column = "billing_cycle", property = "billingCycle"), //计费周期
            @Result(column = "amount_receivable", property = "amountReceivable"), //应收金额
            @Result(column = "collection_time", property = "collectionTime"), //应收时间

            @Result(column = "room_id", property = "room.id"), //所属房屋
            @Result(column = "building_num", property = "room.buildingNum"), //楼号
            @Result(column = "unit", property = "room.unit"), //单元
            @Result(column = "floor", property = "room.floor"), //楼层
            @Result(column = "room_num", property = "room.roomNum"), //房间号
            @Result(column = "building_num", property = "buildingNum"), //楼号
            @Result(column = "unit", property = "unit"), //单元
            @Result(column = "floor", property = "floor"), //楼层
            @Result(column = "room_num", property = "roomNum"), //房间号

            @Result(column = "landlord_id", property = "landlord.id"),//业主类
            @Result(column = "name", property = "landlord.name"),
            @Result(column = "name",property = "landName"),
            @Result(column = "place", property = "landlord.place"),

            @Result(column = "charge_conf_id", property = "chargeConfig.id"), //账单类目
            @Result(column = "charge_project", property = "chargeConfig.chargeProject"),//名称
            @Result(column = "unit_price", property = "chargeConfig.unitPrice"), // 单价
            @Result(column = "charge_project", property = "chargeProject"),//名称

            @Result(column = "payment_method_id", property = "payMentMethod.id"), //支付方式类
            @Result(column = "method", property = "payMentMethod.method"),
            @Result(column = "method", property = "method")
    })
    List<PayMentRecords> getAllPayMR();

    //账单编号模糊查询
    @Select("SELECT * FROM wys_payment_records wpr, wys_landlord wld, wys_charge_config wcc, wys_payment_method wpm, wys_room wr WHERE wpr.landlord_id = wld.id AND wpr.charge_conf_id = wcc.id AND wpr.room_id = wr.id AND wpr.payment_method_id = wpm.id AND bill_number LIKE concat('%',concat(#{billNumber},'%'))")
    List<PayMentRecords> getbillNumber(String billNumber);

    //多条件分页查询--
    @SelectProvider(type = PaySqlProvider.class, method = "findPayment")
    @ResultMap("payMentResultMap")
    List<PayMentRecords> findPayMent(PayMentVo payMentVo);


    public static class PaySqlProvider {
        public String findPayment(PayMentVo pVo){
            //Integer landlordId = pVo.getPayMentRecords().getLandlord().getId();
            //创建动态SQL语句
            String sql = new SQL(){
                {
                    SELECT("*");
                    FROM("wys_payment_records wpr, wys_landlord wld, wys_charge_config wcc, wys_payment_method wpm, wys_room wr");
                    WHERE("wpr.landlord_id = wld.id AND wpr.charge_conf_id = wcc.id AND wpr.room_id = wr.id AND wpr.payment_method_id = wpm.id");

                    if (pVo.getBillNum() != null & pVo.getBillNum().length() != 0){
                        WHERE("bill_number LIKE concat('%',concat(#{billNum},'%'))");
                    }
                    if(pVo.getTHouse() != null & pVo.getTHouse().length() != 0){
                        WHERE("CONCAT(wr.building_num, wr.unit, wr.floor, wr.room_num) LIKE concat('%',concat(#{tHouse},'%'))");
                    }
                    if(pVo.getLName() != null){
                        WHERE("wld.name LIKE concat('%',concat(#{lName},'%'))");
                    }
                    if (pVo.getCcId() != 0){
                        WHERE("charge_conf_id = #{ccId} AND wcc.status = 'y'");
                    }
                    if (pVo.getPmId() != 0){
                        WHERE("payment_method_id = #{pmId}");
                    }
                    if (Objects.equals(pVo.getBilSta(), "1")){
                        WHERE("billing_status = #{bilSta}");
                    } else if (Objects.equals(pVo.getBilSta(), "2")) {
                        WHERE("billing_status = #{bilSta}");
                    }

                    if(Objects.equals(pVo.getSort(), "ascending")){
                        ORDER_BY("collection_time ASC");
                    }else if (Objects.equals(pVo.getSort(), "descending")){
                        ORDER_BY("collection_time DESC");
                    }
                }
            }.toString();
            return sql;
        }
    }
}
